<!--
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-09-11 18:13:17
 * @LastEditors: voanit
 * @LastEditTime: 2022-09-11 18:40:35
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {}

    img {
      float: left;
    }

    .targetDom {
      float: right;
    }

    .box2 {
      width: 500px;
      height: 500px;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div class="box">
    <img src="./back.gif" draggable="true" alt="">
    <!-- <span draggable="true">span</span> -->


    <div draggable="true" class="targetDom" style="width:300px;height:300px;border:3px  solid  red"></div>
  </div>

  <div class="box2"></div>





  <script>
    /**
     * @name: 
     * @return {*}  被拖拽元素【img】   目标元素【图片需要拖到哪里去】
     * ondragstart
     * 
     * 
     * 
     */

    var img = document.querySelector('img')
    var targetDom = document.querySelector('.targetDom')

    /* 定义一个元素：专门用于获取被拖拽的元素 */
    var target = null

    /* 被拖拽元素的事件 */
    window.ondragstart = function (e) {
      /* 只要一开始拖拽就触发 */
      console.log(e, 'ondragstart');
      target = e.target // 获取img dom元素
    }
    img.ondragleave = function () {
      /* 鼠标离开图片的范围时候触发 */
      // console.log('ondragleave');
    }

    img.ondragover = function () {
      /* 持续触发 */
      // console.log('ondragover');
    }
    img.ondragend = function () {
      /* 松开鼠标结束的时候触发 */
      // console.log('ondragend');

    }

    /* 目标元素事件 */
    targetDom.ondragenter = function () {
      /* 进入目标元素的时候就触发 */
      console.log('ondragenter');
    }
    targetDom.ondragleave = function () {
      /* 离开目标元素的时候就触发 */
      console.log('ondragleave');
    }


    window.ondragover = function (e) {
      e.preventDefault();
      //* 持续触发 */
      console.log('ondragover');
    }
    window.ondrop = function (e) {
      /* 松开鼠标的时候就触发  想触发drop就必须在 ondragover阻止默认行为 */
      console.log(e.target, 'ondrop');
      e.target.appendChild(target)

    }
  </script>
</body>

</html>